package com.example.myfirst.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.myfirst.dao.CategoryDao;
import com.example.myfirst.entity.Category;
import com.example.myfirst.service.CategoryService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;

    @ApiModelProperty("查询所有系列")
    @Override
    public List<Category> queryAllCategory() {
        List<Category> categoryList = categoryDao.selectList(null);
        return categoryList;
    }

    @ApiModelProperty("根据id查找系列信息")
    @Override
    public List<Category> queryCategoryById(Integer id) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", id);
        return categoryDao.selectList(qw);
    }

    @ApiModelProperty("添加系列")
    @Override
    public Boolean addCategory(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        int i = categoryDao.insert(category);
        return i > 0;
    }

    @ApiModelProperty("根据id删除系列信息")
    @Override
    public Boolean delCategory(Integer id) {
        int i = categoryDao.deleteById(id);
        return i > 0;
    }

    @ApiModelProperty("根据id修改系列信息")
    @Override
    public Boolean updateCategoryById(Category category, Integer id) {
        category.setUpdateTime(LocalDateTime.now());
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", id);

        int i = categoryDao.update(category, qw);
        return i > 0;
    }

    @ApiModelProperty("根据店铺id来获取,店铺里面的系列")
    @Override
    public List<Category> queryCategoryByShopId(Integer shop_id) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("shop_id",shop_id);

        return categoryDao.selectList(qw);
    }
}
